<?php 
namespace app\admin\controller;
use think\Db;
use app\common\controller\Adminbase;
use think\facade\Session;
use app\admin\model\Gtask as TaskModel;
use app\admin\model\Product as ProductModel;
use app\admin\model\Procedure as ProcedureModel;
use app\admin\model\AdminUser;
use app\admin\model\Gongdan as GongdanModel;
use app\admin\model\Booking as BookingModel;
class Task extends Adminbase
{
    public function index()
    {
        
        if ($this->request->isAjax()) {
            $this->modelClass = new TaskModel();
            list($page, $limit, $where) = $this->buildTableParames();
            $_list = $this->modelClass->where($where)->where('deleted',0)->order('id DESC')->page($page, $limit)->select();
            $status = [
                '未开始','执行中','已取消','已结束','撤回'
            ];
            foreach($_list as $k=>$row){
                $product = ProductModel::find($row['product_id']);
                $_list[$k]['product_nums'] = $product['nums'];
                $_list[$k]['product_title'] = $product['title'];
                $_list[$k]['product_guige'] = $product['guige'];
                $procedure = ProcedureModel::find($row['procedure_id']);
                $_list[$k]['procedure_nums'] = $procedure['nums'];
                $_list[$k]['procedure_title'] = $procedure['title'];
                $_list[$k]['ratio'] = $procedure['ratio'];
                $_list[$k]['auth'] = AdminUser::authProcedure($procedure['auth']);
                $gongdan = GongdanModel::find($row['gongdan_id']);  
                $_list[$k]['total'] = $gongdan['total'];
                $_list[$k]['gongdan_nums'] = $gongdan['nums'];
                $_list[$k]['status'] = $status[$row['status']];
                $_list[$k]['fenpei'] = AdminUser::authProcedure($row['fenpei']);
                //数量
                $bookingList = BookingModel::where('task_id',$row['id'])->select();
                foreach($bookingList as $kk=>$row2){
                    $good = json_decode($row2['good'],true);
                    foreach($good as $k2=>$row3){
                        $_list[$k]['good'] += $row3['nums'];
                    }
                    $bad = json_decode($row2['bad'],true);
                    foreach($bad as $k2=>$row3){
                        if($row3){
                            $badList = json_decode($row3,true);
                            foreach($badList as $k3=>$row4){
                                $_list[$k]['bad'] += (int)$row4['nums'];
                            }
                        }
                    }
                }
            }
            $count = $this->modelClass->where($where)->where('deleted',0)->count();
            $result = array("code" => 0, 'count' => $count, "data" => $_list);
            return json($result);
        }
        return $this->fetch();
    }

    //详情
    public function detail()
    {
        $id = $this->request->param('id');
        $task = TaskModel::find($id);
        //工单信息
        $gongdan = GongdanModel::find($task['gongdan_id']);
        //工序信息 
        $procedure = ProcedureModel::find($task['procedure_id']);
        //产品信息
        $product = ProductModel::find($task['procedure_id']);
        $plan = json_decode($gongdan['plan'],true);
        // dg($task);
        $data = [
            'gongdan_nums'=>$gongdan['nums'],
            'procedure_title'=>$procedure['title'],
            'ratio'=>$procedure['ratio'],
            'actual_begin_time'=>$task['actual_begin_time'],
            'actual_end_time'=>$task['actual_end_time'],
            'auth'=>AdminUser::authProcedure($procedure['auth']),
            'procedure_nums'=>$procedure['nums'],
            'product_title'=>$product['title'],
            'product_nums'=>$product['nums'],
            'plan_begin_time'=>$task['plan_begin_time'],
            'plan_end_time'=>$task['plan_end_time'],
            'plan'=>$plan,
            'good'=>$task['good'],
            'bad'=>$task['bad'],
        ];
        // dg($data);
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function edit()
    {
        if($this->request->post()){
            $data = $this->request->param();
            $attr = $data['attr'];
            $total = 0;
            $plan = [];
            foreach($attr as $k=>$row){
                $plan[] = ['key'=>$row,'nums'=>$data['nums'][$k]];
                $total += $data['nums'][$k];
            }
            if($total == 0){
                $this->error('请添加数量');
            }
            TaskModel::where('id',$data['id'])->update([
                'up_admin'=>session('admin_user_auth')['uid'],
                'ratio'=>$data['ratio'],
                'total'=>$total,
                'plan_begin_time'=>$data['plan_begin_time'],
                'plan_end_time'=>$data['plan_end_time'],
                'plan'=>json_encode($plan),
            ]);
            $this->success('编辑成功');
        }
        $id = $this->request->param('id');
        $task = TaskModel::find($id);
        $gongdan_id = $task['gongdan_id'];
        $gongdan = GongdanModel::find($gongdan_id);
        $product = ProductModel::find($task['product_id']);
        $procedure = ProcedureModel::find($task['procedure_id']);
        $auth = AdminUser::authProcedure($procedure['auth']);
        $plan = json_decode($gongdan['plan'],true);
        $data = [
            'id'=>$task['id'],
            'gongdan_nums'=>$gongdan['nums'],
            'product_nums'=>$product['nums'],
            'product_title'=>$product['title'],
            'procedure_nums'=>$procedure['nums'],
            'procedure_title'=>$procedure['title'],
            'auth'=>$auth,
            'ratio'=>$procedure['ratio'],
            'total'=>$gongdan['total'],
            'total'=>$gongdan['total'],
            'good'=>$task['good'],
            'bad'=>$task['bad'],
            'plan'=>$plan,
            'plan_begin_time'=>$task['plan_begin_time'],
            'plan_end_time'=>$task['plan_end_time'],
            'actual_begin_time'=>$task['actual_begin_time'],
            'actual_end_time'=>$task['actual_end_time'],
        ];
        $this->assign('data',$data);
        return $this->fetch();
    }

    //分配任务
    public function fenpei()
    {
        if($this->request->post()){
            $data = $this->request->param();
            Db::name('gd_gtask')->where('id',$data['id'])->update([
                'fenpei'=>$data['fenpei']
            ]);
            $this->success('分配成功');
        }
        $id = $this->request->param('id');
        //任务ID
        $task = TaskModel::find($id);
        $procedure = ProcedureModel::find($task['procedure_id']);
        //拥有的权限
        $auth = explode(',',$procedure['auth']);
        
        if(in_array(0,$auth)){  //所有人
            $userList = AdminUser::where('status',1)->field('id,nickname')->select();
        }else{
            $userList = AdminUser::where(['status'=>1,'id'=>$auth])->field('id,nickname')->select();
        }
        $this->assign('userlist',$userList);
        $this->assign('id',$id);
        return $this->fetch();
    }
    public function delete()
    {
        $id = $this->request->param('id');
        TaskModel::where('id',$id)->update([
            'deleted'=>1,
            'mdtime'=>date('Y-m-d H:i:s')
        ]);
        $this->success('删除成功');
    }
}